// 硬件数据收集模块


const si = require('systeminformation'); // 引入 systeminformation 模块，用于获取硬件信息
const {machineIdSync} = require('node-machine-id');
const os = require('os'); // 引入 os 模块
const {exec} = require('child_process'); // 引入 child_process 模块

async function readSystemInfo() {
    function Constructor(machineIDSync, cpu, gpu, memory, storage, motherboard, macAddress, success, message) {
        this.machineIDSync = machineIDSync;
        this.cpu = cpu; // CPU 信息
        this.gpu = gpu; // GPU 信息
        this.memory = memory; // 内存信息
        this.storage = storage; // 存储信息
        this.motherboard = motherboard; // 主板信息
        this.macAddress = macAddress; // MAC 地址
        this.success = success; // 操作是否成功的标志
        this.message = message; // 返回的信息或错误消息
    }

    try {
        const cpuInfo = await si.cpu();
        const memInfo = await si.mem();
        const diskInfo = await si.diskLayout();
        const motherboardInfo = await si.baseboard();
        const uniqueId = machineIdSync(); // 获取唯一码
        const gpuModel = await getGpuModel(); // 获取有效的显卡型号
        const memorySize = `${(memInfo.total / (1024 ** 3)).toFixed(2)} GB`; // 只保留内存大小
        const storage = diskInfo.map(disk => `${(disk.size / (1024 ** 3)).toFixed(2)} GB`).join('+');
        const motherboardDetails = motherboardInfo.model; // 只保留主板型号
        // 获取 MAC 地址
        const macAddress = getMacAddress();
        const hardwareInfo = new Constructor(
            uniqueId,
            `${cpuInfo.manufacturer} ${cpuInfo.brand}`,
            gpuModel,
            memorySize, // 内存大小
            storage,
            motherboardDetails,
            macAddress, // 添加 MAC 地址
            true,
            '获取硬件信息成功'
        );
        return hardwareInfo;
    } catch (error) {
        return new Constructor(null, null, null, null, null, null, null, false, '获取硬件信息失败: ' + error.message);
    }
}

// 获取有效的显卡型号
async function getGpuModel() {
    return new Promise((resolve, reject) => {
        exec('wmic path win32_VideoController get name', (error, stdout, stderr) => {
            if (error) {
                console.error('Error executing WMIC:', stderr); // 打印错误信息
                resolve('未识别的显卡'); // 返回默认值
            } else {
                const lines = stdout.trim().split('\n');
                const gpuNames = lines.slice(1)
                    .map(line => line.trim())
                    .filter(line => line !== '' && line !== 'OrayIddDriver Device'); // 过滤掉指定显卡
                const gpuName = gpuNames.length > 0 ? gpuNames.join(', ') : '未识别的显卡'; // 返回有效显卡名称
                resolve(gpuName);
            }
        });
    });
}

// 获取 MAC 地址的函数
function getMacAddress() {
    const networkInterfaces = os.networkInterfaces();
    let macAddress = null;

    for (const interfaceName in networkInterfaces) {
        for (const iface of networkInterfaces[interfaceName]) {
            // 选择非内环接口，并且有有效的 MAC 地址
            if (iface.family === 'IPv4' && !iface.internal) {
                macAddress = iface.mac;
                break;
            }
        }
        if (macAddress) break;
    }

    return macAddress;
}

module.exports = {readSystemInfo};
